package com.sunwayworld.basemodule.rm.archivesmanage.service.impl;

import com.sunwayworld.basemodule.rm.archivesmanage.bean.LimsArchivesHandleBean;
import com.sunwayworld.basemodule.rm.archivesmanage.bean.LimsArchivesManageBean;
import com.sunwayworld.basemodule.rm.archivesmanage.dao.LimsArchivesManageDao;
import com.sunwayworld.basemodule.rm.archivesmanage.service.LimsArchivesHandleService;
import com.sunwayworld.basemodule.rm.archivesmanage.service.LimsArchivesManageService;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.audit.aunnotation.Audit;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import com.sunwayworld.framework.support.PersistableMetadataHelper;
import com.sunwayworld.module.sys.metadata.service.CoreTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsArchivesManageServiceImpl implements LimsArchivesManageService {

    @Autowired
    private LimsArchivesManageDao limsArchivesManageDao;

    @Autowired
    @Lazy
    private LimsArchivesHandleService limsArchivesHandleService;

    @Override
    @SuppressWarnings("unchecked")
    public LimsArchivesManageDao getDao() {
        return limsArchivesManageDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsArchivesManageBean limsArchivesManage = jsonWrapper.parseUnique(LimsArchivesManageBean.class);
        limsArchivesManage.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsArchivesManage);
        return limsArchivesManage.getId();
    }

    @Transactional
    @AuditTrailEntry(AuditTrailType.DELETE)
    @Audit("AUDIT.DELETE")
    public void delete(RestJsonWrapperBean wrapper) {
        List<LimsArchivesManageBean> deleteList = wrapper.parse(this.getDao().getType());
        if (!deleteList.isEmpty()) {

            List<Long> archIds = deleteList.stream().map(LimsArchivesManageBean::getId).collect(Collectors.toList());
            deleteList = this.selectListByIds(archIds);
            List<String> archCodes = deleteList.stream().map(LimsArchivesManageBean::getArchivesCode).filter(Objects::nonNull).distinct().collect(Collectors.toList());
            List<LimsArchivesHandleBean> handleBeans = limsArchivesHandleService.selectListByFilter(SearchFilter.instance().match("archivesCode", archCodes).filter(MatchPattern.OR));
            if (!handleBeans.isEmpty()) {
                limsArchivesHandleService.getDao().deleteBy(handleBeans, "id");
            }

            Class<LimsArchivesManageBean> itemType = this.getDao().getType();
            CoreTableService tableService = ApplicationContextHelper.getBean(CoreTableService.class);
            deleteList.forEach((t) -> {
                tableService.deleteCascade(PersistableMetadataHelper.getTableName(itemType), (Serializable) t.getId());
            });
        }
    }
}
